package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo13 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入老师名字:");
        String name = scan.nextLine();
        try (Connection connection = DBUtil.getConnection();){
            String sql = "SELECT t.name,\n" +
                    "       t.salary,\n" +
                    "       t.title,\n" +
                    "       c.name,\n" +
                    "       c.floor,\n" +
                    "       s.name,\n" +
                    "       s.age,\n" +
                    "       s.gender\n" +
                    "FROM teacher t\n" +
                    "         JOIN class c ON t.id = c.teacher_id\n" +
                    "         JOIN student s ON c.id = s.class_id\n" +
                    "WHERE t.name = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,name);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                String tName = rs.getString("t.name");
                int tSalary = rs.getInt("t.salary");
                String tTitle = rs.getString("t.title");
                String cName = rs.getString("c.name");
                String cFloor = rs.getString("c.floor");
                String sName = rs.getString("s.name");
                String sAge = rs.getString("s.age");
                String sGender = rs.getString("s.gender");
                System.out.println(tName+"--"+tSalary+"--"+tTitle+"--"+cName+"--"+cFloor+"--"+sName+"--"+sAge+"--"+sGender);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
